package com.cyt.web.controller.work.divideclass;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.divideclass.dao.DivideclassClassDao;
import com.cyt.work.divideclass.dao.DivideclassStudentDao;
import com.cyt.work.divideclass.model.po.Divideclass;
import com.cyt.work.divideclass.model.po.DivideclassClass;
import com.cyt.work.divideclass.model.po.DivideclassRule;
import com.cyt.work.divideclass.model.po.DivideclassStudent;
import com.cyt.work.divideclass.model.vo.DivideclassClassVo;
import com.cyt.work.divideclass.model.vo.DivideclassRuleVo;
import com.cyt.work.divideclass.model.vo.DivideclassStudentVo;
import com.cyt.work.divideclass.model.vo.DivideclassVo;
import com.cyt.work.divideclass.service.DivideclassClassService;
import com.cyt.work.divideclass.service.DivideclassRuleService;
import com.cyt.work.divideclass.service.DivideclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 智能分班-智能分班控制层
 *
 * @author caideyong
 * @date 2019-12-20 13:54:04
 */
@RestController
public class DivideclassController extends BaseController {

    @Resource
    private DivideclassService divideclassService;
    @Autowired
    private DivideclassRuleService divideclassRuleService;
    @Autowired
    private DivideclassClassService divideclassClassService;
    @Autowired
    private DivideclassStudentDao divideclassStudentDao;
    @Autowired
    private DivideclassClassDao divideclassClassDao;

    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId  应用id
     * @return Model
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @RequestMapping("/divideclass/divideclass/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/" + pageName);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param divideclassVo 接受参数对象
     * @param page          当前页面
     * @param limit         页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/page", method = RequestMethod.POST)
    public ReturnResult page(DivideclassVo divideclassVo, int page, int limit) {
        return divideclassService.page(divideclassVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String applyId, String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/divideclassEdit");
        view.addObject("applyId", applyId);
        view.addObject("parentMenuId", parentMenuId);
        if (StringUtils.isNotBlank(id)) {
            Divideclass divideclass = divideclassService.selectByPrimaryKey(id);
            view.addObject("divideClass", divideclass);
            view.addObject("divideNum", divideclass.getDivideNum());//分班人数
            DivideclassRuleVo divideclassRuleVo = new DivideclassRuleVo();
            divideclassRuleVo.setDivideId(id);
            List<DivideclassRule> divideclassRuleList = divideclassRuleService.listExample(divideclassRuleVo);
            if (divideclassRuleList != null && divideclassRuleList.size() > 0) {
                view.addObject("divideClassRule", divideclassRuleList.get(0));
            }
            DivideclassClassVo divideclassClassVo = new DivideclassClassVo();
            divideclassClassVo.setDivideId(id);
            List<DivideclassClass> divideclassClassList = divideclassClassDao.getDivideclassClassListNew(divideclassClassVo);
            if (divideclassClassList != null && divideclassClassList.size() > 0) {
                view.addObject("divideClassClassList", divideclassClassList);
            }
        }
        return view;
    }

    /**
     * 跳转详情界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "跳转详情界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getDetail", method = RequestMethod.GET)
    public ModelAndView getDetail(String id, String applyId, String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/divideclassDetail");
        view.addObject("applyId", applyId);
        view.addObject("parentMenuId", parentMenuId);
        if (StringUtils.isNotBlank(id)) {
            Divideclass divideclass = divideclassService.selectByPrimaryKey(id);
            view.addObject("divideClass", divideclass);
            view.addObject("divideNum", divideclass.getDivideNum());//分班人数
            DivideclassRuleVo divideclassRuleVo = new DivideclassRuleVo();
            divideclassRuleVo.setDivideId(id);
            List<DivideclassRule> divideclassRuleList = divideclassRuleService.listExample(divideclassRuleVo);
            if (divideclassRuleList != null && divideclassRuleList.size() > 0) {
                view.addObject("divideClassRule", divideclassRuleList.get(0));
            }
            DivideclassClassVo divideclassClassVo = new DivideclassClassVo();
            divideclassClassVo.setDivideId(id);
            List<DivideclassClass> divideclassClassList = divideclassClassDao.getDivideclassClassListNew(divideclassClassVo);
            if (divideclassClassList != null && divideclassClassList.size() > 0) {
                view.addObject("divideClassClassList", divideclassClassList);
            }
        }
        return view;
    }

    @BaseLog(module = "智能分班", classz = "智能分班", methods = "跳转详情界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getDetailList", method = RequestMethod.GET)
    public ModelAndView getDetailList(String id, String applyId, String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/divideclassResultDetail");
        view.addObject("applyId", applyId);
        view.addObject("parentMenuId", parentMenuId);
        if (StringUtils.isNotBlank(id)) {
            view.addObject("divideId", id);
            Divideclass divideclass = divideclassService.selectByPrimaryKey(id);
            if (divideclass != null && StringUtils.isNotEmpty(divideclass.getResultStatus())) {
                view.addObject("resultStatus", divideclass.getResultStatus());
            }
        }
        return view;
    }

    @BaseLog(module = "智能分班", classz = "智能分班", methods = "跳转用户管理界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getUser", method = RequestMethod.GET)
    public ModelAndView getDivideUser(String divideId, String applyId, String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/divideclassUserList");
        view.addObject("applyId", applyId);
        view.addObject("parentMenuId", parentMenuId);
        if (StringUtils.isNotBlank(divideId)) {
            view.addObject("divideId", divideId);
        }
        return view;
    }

    @BaseLog(module = "智能分班", classz = "智能分班", methods = "跳转统计界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getTj", method = RequestMethod.GET)
    public ModelAndView getTj(String id, String applyId, String parentMenuId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclass/divideclassTj");
        view.addObject("applyId", applyId);
        view.addObject("parentMenuId", parentMenuId);
        if (StringUtils.isNotBlank(id)) {
            view.addObject("divideId", id);
            //各班级男女生列表
            DivideclassClassVo divideclassClassVo = new DivideclassClassVo();
            divideclassClassVo.setDivideId(id);
            List<DivideclassClass> divideclassClassList = divideclassClassService.listExample(divideclassClassVo);
            if (divideclassClassList != null && divideclassClassList.size() > 0) {
                String className = divideclassClassList.get(0).getName();
                String classBoyNum = divideclassClassList.get(0).getBnum();
                String classGirlNum = divideclassClassList.get(0).getGnum();
                Integer boyAll = Integer.parseInt(classBoyNum);
                Integer girlAll = Integer.parseInt(classGirlNum);
                if (divideclassClassList.size() >= 1) {
                    for (int i = 1; i < divideclassClassList.size(); i++) {
                        className = className + "," + divideclassClassList.get(i).getName();
                        classBoyNum = classBoyNum + "," + divideclassClassList.get(i).getBnum();
                        classGirlNum = classGirlNum + "," + divideclassClassList.get(i).getGnum();
                        boyAll = boyAll + Integer.parseInt(divideclassClassList.get(i).getBnum());
                        girlAll = girlAll + Integer.parseInt(divideclassClassList.get(i).getGnum());
                    }
                    view.addObject("classBoyNum", classBoyNum);
                    view.addObject("classGirlNum", classGirlNum);
                    //总男女生
                    view.addObject("boyAll", boyAll.toString());
                    view.addObject("girlAll", girlAll.toString());
                } else {
                    view.addObject("classBoyNum", classBoyNum);
                    view.addObject("classGirlNum", classGirlNum);
                    //总男女生
                    view.addObject("boyAll", boyAll.toString());
                    view.addObject("girlAll", girlAll.toString());
                }
                view.addObject("className", className);
            }
            //查询学生生源地
            DivideclassStudentVo divideclassStudentVo = new DivideclassStudentVo();
            divideclassClassVo.setDivideId(id);
            List<DivideclassStudent> divideclassStudentList = divideclassStudentDao.getDivideclassStudentNum(divideclassStudentVo);
            if (divideclassStudentList != null && divideclassStudentList.size() > 0) {
                String schoolName = divideclassStudentList.get(0).getSchool();
                String schoolNum = divideclassStudentList.get(0).getSchoolNum();
                if (divideclassStudentList.size() >= 1) {
                    for (int j = 1; j < divideclassStudentList.size(); j++) {
                        schoolName = schoolName + "," + divideclassStudentList.get(j).getSchool();
                        schoolNum = schoolNum + "," + divideclassStudentList.get(j).getSchoolNum();
                    }
                    view.addObject("schoolNum", schoolNum);
                } else {
                    view.addObject("schoolNum", schoolNum);
                }
                view.addObject("schoolName", schoolName);
            }
        }
        return view;
    }


    /**
     * 保存或更新
     *
     * @param request       请求对象
     * @param divideclassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, DivideclassVo divideclassVo) {
        return divideclassService.addOrUpdate(divideclassVo, acceptCurrentUser(request));
    }

    @BaseLog(module = "智能分班", classz = "智能分班", methods = "查询分班信息", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/getDivide", method = RequestMethod.POST)
    public ReturnResult getDivide(DivideclassVo divideclassVo) {
        return new ReturnResult(divideclassService.selectByPrimaryKey(divideclassVo.getId()));
    }

    /**
     * 发布分班结果
     *
     * @param request       请求对象
     * @param divideclassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "发布分班结果", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/fbResult", method = RequestMethod.POST)
    public ReturnResult fbResult(HttpServletRequest request, DivideclassVo divideclassVo) {
        return divideclassService.fbResult(divideclassVo, acceptCurrentUser(request));
    }

    @BaseLog(module = "智能分班", classz = "智能分班", methods = "导入用户信息", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/importData", method = RequestMethod.POST)
    public ReturnResult importData(HttpServletRequest request, MultipartFile file, String catalog, String update, String divideId, String applyId) {
        if (StringUtils.isNotEmpty(divideId)) {
            applyId = divideId.split(",")[1];
            divideId = divideId.split(",")[0];
            return divideclassService.importData(request, file, catalog, update, applyId, divideId, acceptCurrentUser(request));
        } else {
            return new ReturnResult(ErrorCode.CODE_1, "参数有误，请联系管理员！");
        }
    }

    /**
     * 开始分班
     *
     * @param request       请求对象
     * @param divideclassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "开始分班", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/divideClass", method = RequestMethod.POST)
    public ReturnResult divideClass(HttpServletRequest request, DivideclassVo divideclassVo) {
        return divideclassService.divideClass(divideclassVo, acceptCurrentUser(request));
    }

    /**
     * 导入学生成绩
     *
     * @param divideId 分班基础id
     * @param applyId  应用id
     * @return ReturnResult
     * @author caideyong
     * @date 2020/1/3
     */
    @RequestMapping(value = "/divideclass/divideclass/importExcel", method = RequestMethod.POST)
    public ReturnResult importExcel(MultipartFile file, HttpServletRequest request, String catalog, String divideId, String applyId) {
        return divideclassService.importExcel(request, file, catalog, acceptCurrentUser(request), divideId, applyId);
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request       请求对象
     * @param divideclassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, DivideclassVo divideclassVo) {
        return divideclassService.remove(divideclassVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request       请求对象
     * @param divideclassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 13:54:04
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, DivideclassVo divideclassVo) {
        return divideclassService.delete(divideclassVo, acceptCurrentUser(request));
    }

    /**
     * 获取菜单树（用于班级管理）
     *
     * @param divideclassClassVo 接受参数对象
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:24
     */
    @BaseLog(module = "智能分班", classz = "智能分班", methods = "菜单树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclass/dTreeListClass", method = RequestMethod.POST)
    public ReturnResult dTreeListClass(DivideclassClassVo divideclassClassVo) {
        return divideclassClassService.dTreeListClass(divideclassClassVo);
    }
}